package larpplanner.logic;

import java.util.List;

import larpplanner.database.*;
import larpplanner.gui.PlannerMain;

/*
 * LARPManager logic implementation
 */
public class LARPManagerImp implements LARPManager {
		    
    static LARPManagerImp instance = null;
    final DBHandler db;

    public static void init(String hostAdrr, short port, String scheme, String username, String password) {
        if (instance == null)
            instance = new LARPManagerImp(hostAdrr, port, scheme, username, password);
    }
    
    public static LARPManager get() {
    	if (instance == null)
            instance = new LARPManagerImp(PlannerMain.HOST_ADRR, PlannerMain.PORT, PlannerMain.SCHEME, PlannerMain.USERNAME, PlannerMain.PASSWORD);
        return instance;
    }
    
	private LARPManagerImp(String hostAdrr, short port, String scheme, String username, String password) {
		db = new DBHandler(hostAdrr, port, scheme, username, password);
	}

	/**
	 * @return a list of QuestSummary of the recent quests in the Quest Selection Window
	 */
	public List<QuestSummary> getQuests() {
		return db.getList(QuestSummary.class, true, null, "-"+Quest.DATE_COL);
	}
	
	public boolean signUp(Player player) {
		long res = db.put(Player.class, player);
		return res!=-1;
	}
	
	/**
	 * @param email - the username (unique email) of the player
	 * @param password - the password of the user
	 * @return if(player email exist && password correct) return the id of the user. else null.
	 */
    public Long signIn(String email, String password) {
        Player signer = db.get(Player.class, Player.EMAIL_COL + "='" + email + "'");
        
        // check password
        if (signer == null || !signer.getPass().equals(password))
            return null;
        
        return signer.getId();
    }
	
	/**
	 * remove a quest from recent quests
	 * @return true if removed successfully
	 */
    public <T> boolean del(Class<T> cls, long id) {
    	System.out.println(""+cls + id);
        return db.del(cls, id);
	}
    
    public boolean deleteAll() {
		return db.deleteAll();
	}
	
    public <T> T get(Class<T> cls, long id) {
        return db.get(cls, id);
	}
	
    public <T> List<T> getAll(Class<T> cls) {
        return db.getAll(cls);
    }
    
	public <T> Long add(Class<T> cls, T obj) {
		return db.put(cls, obj);
	}
	
	public <T> boolean update(Class<T> cls, T obj) {
		Long res = db.update(cls, obj);
		if(res == -1)
			return false;
		else
			return true;
	}
	
    public List<CharUniverse> getCharacters(String universe_name) {
        return db.getList(CharUniverse.class, CharUniverse.UNIVERSE_COL + " = '" + universe_name + "' ");
    }

	public QuestSummary summaryFromQuest(Quest quest) {
		return new QuestSummary(quest);
	}
	
	public Long addParticipant(long questID , long playerID , long characterID) {
		return db.put(Participant.class, new Participant(questID, playerID, characterID));
	}
	
	public boolean removeParticipant(long questID , long playerID) {
		db.del(Participant.class, "" + Participant.QUESTID_COL + " = " + questID +
				" AND "+ Participant.PLAYERID_COL + " = " +playerID);
		return true;
	}
	
	public boolean updateDBfromFreebase() {
		boolean isFirstTime = true;
		if( db.hasFreebaseChar() ) isFirstTime = false;
		System.out.println("is first time = "+isFirstTime);
		return Parser.UpdateDB(isFirstTime);
	}

    public DBHandler getDBHandler() {
        return db;
    }
}